package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Resource;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleResourceMapper roleResourceMapper;

    @Override/*角色的分页查询*/
    public PageResponse<RoleVo> findPage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Role> page = roleMapper.slectPage(roleDto);
        log.info("page:{}",page);
        PageResponse<RoleVo> pageResponse = PageResponse.of(page, RoleVo.class);
        return pageResponse;
    }

    @Override/*角色添加*/
    public Boolean insertRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        return  roleMapper.insertRole(role);

    }

    @Override/*根据角色查询选中的资源数据*/
    public Set<String> checkedResources(Long roleId) {
        return  roleMapper.checkedResources(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)/*添加事物,有异常就回滚*/
    public void deleteRoleById(List<String> roleIds) {
        /*1.先删除角色表*/
        roleMapper.deleteRoleById(roleIds);

        /*2.再删除角色对应的权限表*/
        roleResourceMapper.deleteRoleById(roleIds);
    }

    @Override/*根据id修改角色*/
    public Boolean updateRoleById(RoleDto roleDto) {
        /*先批量删除*/
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateRole(role);/*修改角色名字*/
        if(ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){/*如果它资源编号为空,就是没有资源,就直接改名字*/
            return true;/*这里如果没有资源,就直接修改名字,后面不用执行了*/
        }
        /*先删除所有权限,后批量插入*/
        Long id = roleDto.getId();/*先拿到id*/

        List<String> roleIds = Arrays.asList(id.toString());/*将id变成集合,是string类型的*/
        roleResourceMapper.deleteRoleById(roleIds);

        /*拿到编码后弄成集合批量存入到数据库*/
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();

        List<RoleResource> roleResourceList = new ArrayList<>();

        //将数组类型转成roleResource对象,然后存入集合中


        for (String checkedResourceNo : checkedResourceNos) {
            log.info("checkedResourceNo:{}",checkedResourceNo);
            RoleResource roleResource = new RoleResource(id, checkedResourceNo, "0");/*先创建一个对象,再最后存入进去*/

            /*将编码存进去*/
            roleResourceList.add(roleResource);
        }

        roleResourceMapper.insertRoleResource(roleResourceList);
        return true;
    }

    @Override/*下拉框,查询所有*/
    public List<RoleVo> selectRoleAll() {
        List<Role> roles = roleMapper.selectRoleAll();
        List<RoleVo> roleVos = BeanUtil.copyToList(roles, RoleVo.class);/*将实体类变成vo返回*/
        return roleVos;
    }


}
